package com.futao.springbootlearn20220224.redis.scan;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author futao@gmail.com
 * @date 2022/4/14
 */
@Slf4j
@RequestMapping("/redis/scan")
@RestController
public class SetScanDemo {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/set")
    public void set() {
        String[] strings = new String[10000];
        for (int i = 0; i < 10000; i++) {
            strings[i] = String.valueOf(i);
        }
        log.info("start save data");
        Long add = stringRedisTemplate.opsForSet().add("scan:set", strings);
        log.info("end save data: {}", add);

        log.info("start scan set");
        ScanOptions scanOptions = ScanOptions.scanOptions()
                .count(1000)
                .build();
        Cursor<String> scan = stringRedisTemplate.opsForSet().scan("scan:set", scanOptions);
        log.info("end scan set {}", scan.stream().count());
    }
}
